package com.javahash.spring.controller;

import java.util.List;
import org.apache.ibatis.mapping.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.javahash.spring.classes.User;
import com.javahash.spring.service.UserService;

@Controller
public class TableController {
	
		@Autowired
		private UserService userService;
		
		private List<User>  us;
	
		@RequestMapping("/Cz")
		public String Cz(Model model,String txt_id,  String txt_name,String hd, String del_id, String del_name)
		{
			if(hd.equals("add"))
			{
			  User user = new User(Integer.parseInt(txt_id), txt_name);
			  return Add(model, user);
			}
			else
			{
				User user = new User(Integer.parseInt(del_id), del_name);
				if(hd.equals("delete"))
			        return Delete( model,  user);
			}
			
			System.out.println(userService);
			
		    return "hello";
		}
	
		public String Add(Model model, User user)
		{
			try {
				us = (List<User>)userService.add(user);
				model.addAttribute("users",us);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return "Table";
		}
		
		public String Delete(Model model, User user)
		{
			try {
				us = (List<User>) userService.delete(user);
				model.addAttribute("users",us);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return "Table";
		}
		
		@RequestMapping("/Show")
		public String Show(Model model) throws Exception
		{
			List<User> us= userService.show();
//			System.out.println(userService);
//			for(int i=0; i<us.; i++)
//			{
//				User u = new User();
//				u = us.get(i);
//				System.out.println("************"+u.getName()+"**********"+Integer.toString(u.getId()));	
//			}
			
			model.addAttribute("users",us);
			return "Table";
		}
		
		
}
